/**
 * 邮箱
 * @param {*} s
 */
export const isEmail = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('邮件不能为空'))
  }

  if (!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(value)) {
    callback(new Error('邮件格式不正确'))
  }

  callback()
}

/**
 * 手机号码
 * @param {*} s
 */
export const isMobile = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('手机号不能为空'))
  }

  if (!(/^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\d{8}$/.test(value) || /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(value))) {
    callback(new Error('手机号格式不正确'))
  }

  callback()
}

/**
 * URL地址
 * @param {*} s
 */
export const isURL = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('URL不能为空'))
  }

  if (!/^http[s]?:\/\/.*/.test(value)) {
    callback(new Error('URL格式不正确'))
  }

  callback()
}

/**
 * 身份证
 * @param {*} s
 */
export const isIdCard = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('身份证不能为空'))
  }

  if (!/(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)/.test(value)) {
    callback(new Error('身份证格式不正确'))
  }

  callback()
}

/**
 * 转换日期为yyyy-MM-dd HH:mm:ss字符串
 * @param date
 * @returns {string}
 */
export const formatTimeToStr = (date: string) => {
  const transferDate = new Date(date)
  const y = transferDate.getFullYear()
  let MM: string | number = transferDate.getMonth() + 1
  MM = MM < 10 ? '0' + MM : MM
  let d: string | number = transferDate.getDate()
  d = d < 10 ? '0' + d : d
  let h: string | number = transferDate.getHours()
  h = h < 10 ? '0' + h : h
  let m: string | number = transferDate.getMinutes()
  m = m < 10 ? '0' + m : m
  let s: string | number = transferDate.getSeconds()
  s = s < 10 ? '0' + s : s
  return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
}
